<link href="${rc.getContextPath()}/manage/css/dwzCustomCss/dwz.custom.css?v=${ver}" rel="stylesheet" type="text/css">
<style>
.tree .workUnit a {
	color: #4C9ED9;
}
.tree .andonGroup a {
	color: #39AA46;
}
#menuBar .noright span {
	background-position: -470px -695px;
	color: #D8D1D1;
}
</style>
<div class="pageContent">
	<div class="treeleft" style="width: 290px; float: left; border-right: 1px solid #D8DFE5;">
		<div class="mini-toolbar">
			<input type="text" id="workcenterSearch" /> <a class="buttonActive" href="javascript:void(0)" id="searchTree"> <span><@label
					key='search.B'>搜索</@label></span>
			</a>
		</div>
		<div id="menuBar" class="pBar pBarBorder">
			<ul class="toolBar">
				<li>
					<a class="collapse" id="collapsable" title='<@label key="collapseAll.D">全部收起</@label>'> 
						<span><@label key="collapse.B">收起</@label></span>
					</a>
				</li>
				<li>
					<a class="expand" id="expandable" title='<@label key="expandAll.D">全部展开</@label>'> 
						<span><@label key="expand.B">展开</@label></span>
					</a>
				</li>
				<@buttonRight menuCode="ANDONGROUP_ADD" labelKey="add.B" defalutName="新增">
				<li>
					<a class="add" id="addGroup" style="display: none;" title='<@label key="add.B">新增</@label>'> 
						<span><@label key="add.B">新增</@label></span>
					</a>
					<a class='noright' title='只能在作业单元下创建ANDON组'>
						<span><@label key="add.B">新增</@label></span>
					</a>
				</li>
				</@buttonRight>
				<@buttonRight menuCode="ANDONGROUP_RENAME" labelKey="rename.B" defalutName="重命名">
				<li>
					<a class="edit" id="renameGroup" style="display: none;" title='<@label key="rename.B">重命名</@label>'> 
						<span><@label key="rename.B">重命名</@label></span>
					</a>
					<a class='noright' title='只能重命名ANDON组'>
						<span><@label key="rename.B">重命名</@label></span>
					</a>
				</li>
				</@buttonRight>
				<@buttonRight menuCode="ANDONGROUP_DEL" labelKey="delete.B" defalutName="删除">
				<li>
					<a class="delete" id="deleteGroup" style="display: none;" title='<@label key="delete.B">删除</@label>'> 
						<span><@label key="delete.B">删除</@label></span>
					</a>
					<a class='noright' title='只能删除ANDON组'>
						<span><@label key="delete.B">删除</@label></span>
					</a>
				</li>
				</@buttonRight>
			</ul>
		</div>
		<div id="workcenterTree" layoutH="68">
			<#include "andon/tree.html">
		</div>
	</div>
	<div style="margin-left: 291px; margin-top: -1px;" class="pageContent">
		<div id="jGridForAndonList" style="float: left; width: 100%;"></div>
	</div>
</div>
<div id="formBarTemp" style="display: none;" class="formBar">
	<ul>
		<li>
			<div class="buttonActive">
				<div class="buttonContent">
					<button id="save" type="button" name="btnSave"><@label key="save.B">保存</@label></button>
				</div>
			</div>
		</li>
		<li>
			<div class="buttonActiveGrey" style="margin-left: 10px;">
				<div class="buttonContent">
					<button type="button" class="close"><@label key="cancel.B">取消</@label></button>
				</div>
			</div>
		</li>
	</ul>
</div>
<script type="text/javascript">
	(function($) {
		var $page = DWZ.context(), $grid = $('#jGridForAndonList', $page), postData = {},
		$barTemp = $page.find('#formBarTemp');
		
		$grid.jGrid({
			layoutH: 192,
			base: hostUrl + '/andon/',
			url: 'getAndonListByCondition',
			autoLoad: true,
			columns: [{
				field: 'workUnitCode;andonCode',
				group: 'ids',
				type: 'checkbox',
				width: 40
			}, {
				field: 'andonCode',
				text: '<@label key="andonCode.C">ANDON编号</@label>',
				width: 100,
				query: true,
				defaultOrder: 'asc'
			}, {
				field: 'andonText',
				text: '<@label key="andonText.C">ANDON描述</@label>',
				width: 100,
				query: true
			}, {
				field: 'workCenterText',
				text: '工作中心',
				width: 100
			}, {
				field: 'workUnitText',
				text: '作业单元',
				width: 100
			}, {
				field: 'typeText',
				text: '<@label key="andonType.C">ANDON类型</@label>',
				width: 100,
				query: true,
				queryField: 'andonTypeCode',
				queryType: 'select', 
				queryOptions: [
					{value: '', label: '<@label key="all.C">所有</@label>'}
					<#list andonTypes as andonType>
					,{value: "${andonType.andonTypeCode}", label: "${andonType.typeText}"}
					</#list>
				]
			}, {
				field: 'templateText',
				text: '<@label key="andonTemplate.C">ANDON模板</@label>',
				width: 100,
				query: true,
				queryField: 'templateCode',
				queryType: 'select', 
				queryOptions: [
					{value: '', label: '<@label key="all.C">所有</@label>'}
					<#list templates as template>
					,{value: "${template.templateCode}", label: "${template.templateText}"}
					</#list>
				]
			}, {
				field: 'eventText',
				text: '<@label key="event.C">事件</@label>',
				width: 100,
				query: true,
				queryField: 'eventCode'
			}, {
				field: 'ruleText',
				text: '<@label key="andonMessageRule.C">消息通知规则</@label>',
				width: 100,
				query: true,
				queryField: 'ruleCode'
			}, {
				field: 'alarmFlag',
				text: '<@label key="alarm.C">报警</@label>',
				width: 40,
				transform: function(data) {
					if (data == 0) {
						return '<@label key="no.C">否</@label>';
					} else if (data == 1) {
						return '<@label key="yes.C">是</@label>';
					}
				},
				query: true,
				queryType: 'select', 
				queryOptions: [
					{value: '', label: '<@label key="all.C">所有</@label>'},
					{value: 0, label: '<@label key="no.C">否</@label>'},
					{value: 1, label: '<@label key="yes.C">是</@label>'}
				]
			}, {
				field: 'lockFlag',
				text: '<@label key="lockFlag.C">锁定标记</@label>',
				width: 40,
				transform: function(data) {
					if (data == 0) {
						return '<@label key="no.C">否</@label>';
					} else if (data == 1) {
						return '<@label key="yes.C">是</@label>';
					}
				},
				orderTable: 'a',
				query: true,
				queryType: 'select', 
				queryOptions: [
					{value: '', label: '<@label key="all.C">所有</@label>'},
					{value: 0, label: '<@label key="no.C">否</@label>'},
					{value: 1, label: '<@label key="yes.C">是</@label>'}
				]
			}],
			getQueryData: function() {
				return postData;
			},
			gridbars: [{
				icon: 'add',
				text: '<@label key="add.B"></@label>',
				title: '新增ANDON站点',
				target: 'dialog',
				href: 'addAndonPage',
				data: {
					navTabId: "${navTabId!''}"
				},
				preCheck: function($bar) {
					var $this = $bar.closest('li'), data = $this.data();
					if (!data.workUnitCode) {
						this.href = 'addAndonPage';
						alertMsg.error('请选择作业单元或ANDON组节点进行添加ANDON站点！');
						return false;
					}
					this.href = 'addAndonPage?workUnitCode=' + data.workUnitCode + '&andonGroup=' + encodeURIComponent(data.andonGroup || '')
					return true;
				},
				mask: true,
				width: 640,
				height: 430,
				menuCode: 'ANDONSITE_ADD'
			}, {
				icon: 'edit',
				text: '<@label key="edit.B"></@label>',
				title: '编辑ANDON站点',
				target: 'dialog',
				href: 'editAndonPage',
				data: {
					workUnitCode: '{workUnitCode}',
					andonCode: '{andonCode}'
				},
				mask: true,
				width: 640,
				height: 485,
				menuCode: 'ANDONSITE_EDIT'
			}, {
				icon: 'test',
				text: '<@label key="group.B"></@label>',
				title: 'ANDON站点分组',
				target: 'action',
				preCheck: function($bar) {
					var $this = $bar.closest('li'), data = $this.data();
					if (!data.workUnitCode) {
						alertMsg.error('请选择作业单元节点进行ANDON站点分组！');
						return false;
					}
					if (!data.groups.length) {
						alertMsg.error('所选作业单元下没有可用的ANDON组！');
						return false;
					}
					var andons = getSelectedAndons();
					if (!andons.length) {
						alertMsg.error(DWZ.msg('alertSelectMsg'));
						return false;
					}
					this.postData = {
						groups: data.groups,
						andons: andons,
						workUnitCode: data.workUnitCode
					}
					return true;
				},
				action: function() {
					var andonGroups = [], data = this.postData;
					for (var i = 0; i < data.groups.length; i++) {
						andonGroups.push({andonGroup: data.groups[i]});
					}
					$.pdialog.open('', 'groupAndon', 'ANDON分组', {
						max: false,
						mask: true,
						maxable: false,
						minable: false,
						fresh: true,
						resizable: false,
						width: 360,
						height: 300,
						callback: function($content, $dialog) {
							var $groupGrid = $('<div/>').jGrid({
								autoLoad: false,
								inner: true,
								hasBorder: true,
								width: 340,
								height: 200,
								columns: [{
									field: 'andonGroup',
									group: 'ids',
									type: 'checkbox',
									width: 40
								}, {
									field: 'andonGroup',
									text: '<@label key="andonGroup.C">ANDON组</@label>',
									width: 200
								}],
								fillData: andonGroups
							});
							$content.append($groupGrid).append($barTemp.clone().show()).initUI();
							$content.find('.close').click(function(){
								$.pdialog.closeCurrent();
							});
							$content.find('#save').click(function(){
								var selectedGroups = [];
								$groupGrid.find('input[name=ids][type=checkbox]:checked').each(function(){
									selectedGroups.push(this.value);
								});
								if (!selectedGroups.length) {
									alertMsg.error(DWZ.msg('alertSelectMsg'));
								} else {
									DWZ.ajax({
										url: hostUrl + '/andon/assignAndonGroup',
										data: {
											workUnitCode: data.workUnitCode,
											andons: JSON.stringify(data.andons),
											groupos: selectedGroups.join(',')
										},
										success: function () {
											
										}
									});
								}
								$.pdialog.closeCurrent();
							});
						}
					});
				},
				menuCode: 'ANDONSITE_GROUP'
			}, {
				icon: 'add',
				text: '<@label key="copy.B"></@label>',
				title: '复制ANDON站点',
				target: 'dialog',
				href: 'batchOperation?operationType=copy',
				data: {
				},
				preCheck: function($bar) {
					return operateCheck.apply(this,[$bar]);
				},
				mask: true,
				width: 320,
				height: 480,
				menuCode: 'ANDONSITE_COPY'
			}, {
				icon: 'up',
				text: '<@label key="move.B"></@label>',
				title: '移动ANDON站点',
				target: 'dialog',
				href: 'batchOperation?operationType=move',
				data: {
				},
				preCheck: function($bar) {
					return operateCheck.apply(this,[$bar]);
				},
				mask: true,
				width: 320,
				height: 480,
				menuCode: 'ANDONSITE_MOVE'
			}, {
				icon: 'icon',
				text: '<@label key="view.B"></@label>',
				title: 'ANDON站点详情',
				target: 'dialog',
				href: 'viewAndonPage',
				data: {
					workUnitCode: '{workUnitCode}',
					andonCode: '{andonCode}'
				},
				mask: true,
				width: 640,
				height: 485,
				menuCode: 'ANDONSITE_VIEW'
			}, {
				icon: 'delete',
				text: '<@label key="delete.B">删除</@label>',
				title: "确认要删除选择的消息配置吗？",
				target: 'selectedTodo',
				href: 'deleteAndon',
				data: {
					ids: '{ids}'
				},
				menuCode: 'ANDONSITE_DEL'
			}]
		});
		
		var operateCheck = function($bar) {
			var $this = $bar.closest('li'), data = $this.data();
			if (!data.workUnitCode) {
				alertMsg.error('请选择作业单元或ANDON组节点再进行操作进行！');
				return false;
			}
			var andons = getSelectedAndons();
			if (!andons.length) {
				alertMsg.error(DWZ.msg('alertSelectMsg'));
				return false;
			}
			this.data.andons = JSON.stringify(andons);
			this.data.sWorkUnitCode = data.workUnitCode;
			this.data.sAndonGroup = data.andonGroup || '';
			return true;
		};
		
		$("#searchTree", $page).baseTreeSearch({
			inputTextId: "#workcenterSearch",
			alertMsg: "<@label key='Tree_search_Input.D'>输入名称进行查询</@label>", //提示内容，要考虑国际化，传自后台
			$p: $page
		});

		$("#collapsable", $page)._treeCollapsable({
			$p: $page
		})

		$("#expandable", $page)._treeExpandable({
			$p: $page
		});
		
		// 新增组
		var $addGroup = $('#addGroup', $page).click(function(){
			var $this = $(this), data = $this.data(), $parent = data.$ele, nodeNum = $parent.parents('ul').length;
			
			var $group = $('<li class="andonGroup" workunitCode="' + data.workUnitCode + '" andongroup="" />'), $div = $('<div/>');
			
			for (var int = 0; int < nodeNum; int++) {
				$div.append('<div class="line"/>');
			}
			$div.append('<div class="node"/><div class="file"/><input type="text" maxlength="30" value="未命名组01"/>').appendTo($group);
			
			if (!$parent.children('ul').length) {
				$group.addClass('last');
				$parent.append('<ul/>').find('.node').removeClass('node').addClass('collapsable');
			}
			$parent.children('ul').prepend($group);
			
			$div.find('input').focus().blur(function(){
				if (!this.value) {
					if ($group.parent().children('li').length == 1) {
						console.log($group.closest('li'))
						$group.parent().closest('li').children('div').children('.collapsable').removeClass('collapsable').addClass('node');
						$group.parent().remove();
					} else {
						$group.remove();
					}
					return false;
				}
				var andonGroup = this.value, group = {
					workUnitCode: data.workUnitCode,
					andonGroup: andonGroup
				}, existing = validateAndonGroup(group);
				
				if (existing) {
					alertMsg.error('该组名已经存在，请使用其他名称');
					return false;
				}
				DWZ.ajax({
					url: hostUrl + '/andon/addAndonGroup',
					data: group,
					success: function(){
						refreshTree($.extend({type: 'add'}, group))	
					}
				});
			});
		}),
		// 重命名组
		$renameGroup = $('#renameGroup', $page).click(function(){
			var $this = $(this), data = $this.data(), $parent = data.$ele;
			$('<input type="text" maxlength="30" value="' + data.andonGroup + '"/>').insertAfter($parent.children('div').children('a').hide()).focus().blur(function(){
				// 没有改变或者清空组名时保持不变
				if (!this.value || this.value == data.andonGroup) {
					$(this).prev().show();
					$(this).remove();
					return false;
				}
				var andonGroup = this.value, existing = validateAndonGroup({
					workUnitCode: data.workUnitCode,
					andonGroup: andonGroup
				});
				if (this.value.toUpperCase() != data.andonGroup.toUpperCase() && existing) {
					alertMsg.error('该组名已经存在，请使用其他名称');
					$(this).remove();
					$parent.children('div').children('a').show();
					return false;
				}
				DWZ.ajax({
					url: hostUrl + '/andon/renameAndonGroup',
					data: {
						workUnitCode: data.workUnitCode,
						originalAndonGroup: data.andonGroup,
						andonGroup: andonGroup
					},
					success: function(){
						refreshTree({type: 'update', workUnitCode: data.workUnitCode, andonGroup: andonGroup})
					}
				});
			});;
		}), 
		// 删除组
		$deleteGroup = $('#deleteGroup', $page).click(function(){
			var $this = $(this), data = $this.data(), _doDelete = function() {
				DWZ.ajax({
					url: hostUrl + '/andon/deleteAndonGroup',
					data: {
						workUnitCode: data.workUnitCode,
						andonGroup: data.andonGroup
					},
					success: function(){
						refreshTree({type: 'delete', workUnitCode: data.workUnitCode})
					}
				});
			};
			alertMsg.confirm('确认要删除该分组吗？', {okCall: _doDelete});
		}),
		// 新增ANDON
		$addAndon = $page.find('#ANDONSITE_ADD'),
		// ANDON分组
		$groupAndon = $page.find('#ANDONSITE_GROUP'),
		// ANDON移动/复制
		$operateAndon = $page.find('#ANDONSITE_MOVE, #ANDONSITE_COPY'),
		// 操作按灯组
		operateAndonGroup = function(data) {
			$addGroup.hide().removeData().next().show();
			$renameGroup.show().data(data).next().hide();
			$deleteGroup.show().data(data).next().hide();
			$addAndon.removeData().data(data);
			$operateAndon.removeData().data(data);
			$groupAndon.removeData();
		},
		// 操作作业单元
		operateWorkUnit = function(data) {
			$addGroup.show().data(data).next().hide();
			$renameGroup.hide().removeData().next().show();
			$deleteGroup.hide().removeData().next().show();
			$addAndon.removeData().data(data);
			$operateAndon.removeData().data(data);
			var groups = [];
			data.$ele.find('.andonGroup').each(function(){
				groups.push($(this).attr('andonGroup'));
			});
			$groupAndon.removeData().data($.extend({groups: groups}, data));
		},
		// 操作工作中心
		operateWorkCenter = function() {
			$addGroup.hide().removeData().next().show();
			$renameGroup.hide().removeData().next().show();
			$deleteGroup.hide().removeData().next().show();
			$addAndon.removeData();
			$operateAndon.removeData();
			$groupAndon.removeData();
		},
		// 检测组名是否存在
		validateAndonGroup = function(data) {
			var isExisting = false;
			DWZ.ajax({
				url: hostUrl + '/andon/validateAndonGroup',
				async: false,
				data: data,
				success: function(json) {
					isExisting = !json;
				}
			})
			return isExisting;
		},
		$treePanel = $('#workcenterTree', $page),
		refreshTree = function(data) {
			operateWorkCenter();
			DWZ.ajax({
				url: hostUrl + '/andon/reloadTree',
				dataType: 'html',
				success: function(html){
					$treePanel.empty().append(html).initUI();
					setTimeout(function() {
						if (data.type === 'delete' || data.type === 'add') {
							$treePanel.find('.workUnit[workunitCode="' + data.workUnitCode + '"]').children('div').children('a').click();
						} else {
							$treePanel.find('.andonGroup[workunitCode="' + data.workUnitCode + '"][andonGroup="' + data.andonGroup + '"]').children('div').children('a').click();
						}
					}, 100);
				}
			});
		},
		getSelectedAndons = function() {
			var andons = [];
			$grid.find('input[name=ids][type=checkbox]:checked').each(function(){
				var data = $(this).closest('tr').data();
				andons.push({
					andonCode: data.andonCode,
					andonText: data.andonText,
					lockFlag: data.lockFlag,
					templateCode: data.templateCode,
					workUnitCode: data.workUnitCode
				});
			});
			return andons;
		};
		
		$treePanel.on('check', '.workCenter, .workUnit, .andonGroup', function(event) {
			var $this = $(this);
			postData = {};
			if ($this.is('.workCenter')) {
				operateWorkCenter();
				postData.workCenterCode = $this.attr('workcenterCode');
				postData.workCenterCodes = [];
				postData.workCenterCodes.push(postData.workCenterCode);
				$this.find('.workCenter').each(function(){
					postData.workCenterCodes.push($(this).attr('workcenterCode'));
				});
				postData.workCenterCodes = postData.workCenterCodes.join(',');
			} else if ($this.is('.workUnit')) {
				operateWorkUnit({workUnitCode: $this.attr('workunitCode'), $ele: $this});
				postData.workUnitCode = $this.attr('workunitCode');
			} else if ($this.is('.andonGroup')) {
				if ($this.is('.temporary')) {
					return false;
				}
				operateAndonGroup({workUnitCode: $this.attr('workunitCode'), andonGroup: $this.attr('andonGroup'), $ele: $this});
				postData.workUnitCode = $this.attr('workunitCode');
				postData.andonGroup = $this.attr('andonGroup');
			}
			
			$grid.jGrid('loadData');
			return false;
		});
		$page.find('.searchContent li:nth-child(3)').css({
			width: 250
		});
	})(jQuery);
</script>